/*
 * ConnectorEditor.java
 *
 */

package flowchart.editors;
import flowchart.nodes.*;
import java.awt.*;

/**
 *
 * @author jl
 */
public class ConnectorEditor implements GraphicsObjectEditor{
    
    protected GraphicsConnector connector;
    
    /** Creates a new instance of GuideEditor */
    public ConnectorEditor() {
    }
    
    public GraphicsObject getGraphicsObject() {
        return connector;
    }
    
    public void setGraphicsObject(GraphicsObject obj) {
         if (obj instanceof GraphicsConnector) {
            connector = (GraphicsConnector) obj;
        } else {
            throw new RuntimeException("Wrong type for editor!");
        }
    }


// Algoritmi jolla lasketaan pisteen etäisyys viivasta
// Alexander Hristovin koodiesimerkki ladattu osoitteesta:
// http://www.ahristov.com/tutorial/geometry-games/point-line-distance.html

    private double pointToLineDistance(Point A, Point B, Point P) {
        double normalLength = Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
        return Math.abs((P.x - A.x) * (B.y - A.y) - (P.y - A.y) * (B.x - A.x)) / normalLength;
    }

    //Check if user clicked near connector
    private boolean hitConnector(int x, int y) {

        Point p1 = connector.getStartP();
        Point p2 = connector.getEndP();
        Double distance = pointToLineDistance(p1, p2, new Point(x, y));

        System.out.println(distance);
        if (distance < 4) {
            return true;
        } else {
            return false;
        }
    }

    public HitTestInfo hitTest(int x, int y) {

        if (hitConnector(x, y)){
               return new HitTestInfo(HitTestInfo.NOHANDLE, 10,10);
        }
        else{
            return new HitTestInfo();
        }

        /*
        Rectangle bounds = connector.getBounds();
        if (bounds.contains(x, y)) {
            return new HitTestInfo(HitTestInfo.NOHANDLE, x - connector.getX(), y - connector.getY());
        } else if (Math.abs(x - connector.getX()) <= connector.getWidth()/2) {
            return new HitTestInfo(1, x - connector.getX(), 0);
        } else if (Math.abs(y - connector.getY()) <= connector.getWidth()/2) {
            return new HitTestInfo(2, 0, y - connector.getY());
        }
        */
        //return new HitTestInfo();
    }
    
    public boolean moveHandle(HitTestInfo hInfo, int nx, int ny) {
        if (hInfo.code == HitTestInfo.NOHANDLE) {
            /*
            connector.setX(nx - hInfo.offsx);
            connector.setY(ny - hInfo.offsy);
        } else if (hInfo.code == 1) {
            connector.setX(nx - hInfo.offsx);
        } else if (hInfo.code == 2) {
            connector.setY(ny - hInfo.offsy);
        }
             */
        }
        return true;
    }
    public boolean snap(){
        return true;
    }
    
    public void drawHandles(Graphics2D g, Color fill, Color border) {
        Color old = g.getColor();
        g.setColor(Color.red);
        g.drawLine(connector.getStartP().x, connector.getStartP().y, connector.getEndP().x, connector.getEndP().y);
        g.setColor(old);
    }
    
}
